package com.training.bt.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.training.bt.entity.UserPoint;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserPointMapper extends BaseMapper<UserPoint> {

    @Select("SELECT * FROM user_point WHERE user_id = #{userId}")
    UserPoint findByUserId(@Param("userId") Long userId);

    @Update("UPDATE user_point " +
            "SET point = point - #{points}, " +
            "    download_bytes = download_bytes + #{size} " +
            "WHERE user_id = #{userId} AND point >= #{points}")
    int deductPoints(@Param("userId") Long userId,
            @Param("points") int points,
            @Param("size") long size);
}